package apply;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import theBeans.exam;
import theBeans.position;
import theDaoFactory.DaoFactory;
import theDaos.IPositionDao;

/**
 * Servlet implementation class payServlet
 */
@WebServlet("/payServlet")
public class payServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public payServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int ifPaySucceed=Integer.parseInt(request.getParameter("ifPaySucceed"));
		if(ifPaySucceed==1)
		{
			response.sendRedirect("");
		}
		//若支付失败，将已分配的pid的ifEmpty置为1，并将总考位数加一
		else
		{
			HttpSession session=request.getSession();
			exam newExam=(exam)session.getAttribute("newExam");
			
			IPositionDao positionDaoInstance=DaoFactory.getPositionDaoInstance();
			position positionOption=new position();
			int result=0;
			int ifEmpty=1;
			result=positionDaoInstance.update(String.valueOf(newExam.getPid()),String.valueOf(ifEmpty));
			ServletContext application=request.getServletContext();
			int positionLeft=((Integer)application.getAttribute("positionLeft"))+1;
			application.setAttribute("positionLeft", positionLeft);
			
			request.setAttribute("errorInfo", "");
			request.setAttribute("toURL", "");
			request.getRequestDispatcher("errorPage.jsp").forward(request, response);
		}
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
